#include "widget.h"
#include "ui_widget.h"
#include "homeroot.h"
#include "database.h"
#include "hometeacher.h"

#include <QMessageBox>
#include <QPushButton>
#include <QDebug>

Widget::Widget(QWidget *parent):QWidget(parent),ui(new Ui::Widget)
{
    ui->setupUi(this);
    HomeRoot *hr = new HomeRoot;
    HomeTeacher *ht = new HomeTeacher;
    DataBase *db = new DataBase;
    if(db->connectDB() == "false")
    {
         QMessageBox::critical(this,"critical","错误");
    }
    //点击登录按钮
    connect(ui->btnLin,&QPushButton::clicked,this,[=](){
        //返回用户姓名和权限
        QString getUser_name = db->logIn_name(ui->userId->text().toUtf8().data(),ui->userPass->text().toUtf8().data());
        QString getUser_per = db->logIn_per(ui->userId->text().toUtf8().data(),ui->userPass->text().toUtf8().data());
        qDebug() << getUser_name+" "+getUser_per;
        //判断权限
        if(getUser_per!="false1")
        {
            if(getUser_per == "r")      //权限等于管理员
            {
                hr->inital(ui->userId->text().toUtf8(),getUser_name);
                this->close();
                hr->show();
            }else if(getUser_per == "t")    //权限等于老师
            {
                hr->inital(ui->userId->text().toUtf8(),getUser_name);
                this->close();
                ht->show();
            }
        }
    });
    connect(ui->btnLout,&QPushButton::clicked,this,[=](){
        db->logOut();
        this->close();
    });
}

Widget::~Widget()
{
    delete ui;
}
